<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="88px"
    >
      <el-form-item label="订单编号" prop="orderNumber">
        <el-input
          v-model="queryParams.orderNumber"
          placeholder="请输入订单编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="货运单编号" prop="waybillNumber">
        <el-input
          v-model="queryParams.waybillNumber"
          placeholder="请输入货运单编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发货人姓名" prop="senderName">
        <el-input
          v-model="queryParams.senderName"
          placeholder="请输入发货人姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发货人电话" prop="senderPhone">
        <el-input
          v-model="queryParams.senderPhone"
          placeholder="请输入发货人电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发货人地址" prop="senderAddr">
        <el-input
          v-model="queryParams.senderAddr"
          placeholder="请输入发货人地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="收货人姓名" prop="receiverName">
        <el-input
          v-model="queryParams.receiverName"
          placeholder="请输入收货人姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="收货人电话" prop="receiverPhone">
        <el-input
          v-model="queryParams.receiverPhone"
          placeholder="请输入收货人电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="收货人地址" prop="receiverAddr">
        <el-input
          v-model="queryParams.receiverAddr"
          placeholder="请输入收货人地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="结算状态" prop="agencyStatus">
        <el-select
          v-model="queryParams.agencyStatus"
          placeholder="请选择结算状态"
          clearable
        >
          <el-option
            v-for="dict in dict.type.agency_fund_status_yes_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['accept:contract:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['accept:contract:export']"
          >导出</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['accept:contract:add']"
          >发起新的订单</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="contractList"
      @selection-change="handleSelectionChange"
      max-height="350"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="订单编号" align="center" prop="orderNumber" />
      <el-table-column
        label="货运单编号"
        align="center"
        prop="waybillNumber"
        width="120"
      />
      <el-table-column
        label="发货人姓名"
        align="center"
        prop="senderName"
        width="100"
      />
      <el-table-column
        label="发货人电话"
        align="center"
        prop="senderPhone"
        width="120"
      />
      <el-table-column
        label="发货人地址"
        align="center"
        prop="senderAddr"
        width="120"
      />
      <el-table-column
        label="收货人姓名"
        align="center"
        prop="receiverName"
        width="100"
      />
      <el-table-column
        label="收货人电话"
        align="center"
        prop="receiverPhone"
        width="100"
      />
      <el-table-column
        label="收货人地址"
        align="center"
        prop="receiverAddr"
        width="120"
      />
      <el-table-column label="结算状态" align="center" prop="agencyStatus">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.agency_fund_status_yes_no"
            :value="scope.row.agencyStatus"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="货运单状态"
        align="center"
        prop="waybillStatus"
        width="100"
      >
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.accept_waybill_status"
            :value="scope.row.waybillStatus"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="物品详情"
        align="center"
        width="80"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-dropdown
            size="mini"
            @command="(command) => handleCommand(command, scope.row)"
            v-hasPermi="['system:user:resetPwd', 'system:user:edit']"
          >
            <span class="el-dropdown-link">
              <i class="el-icon-d-arrow-right el-icon--right"></i>详情
            </span>
            <el-dropdown-menu slot="dropdown">
              <ul style="list-style-type: none; padding: 30px">
                <li>{{ "货物重量：" + scope.row.goodsWeight }}</li>
                <li>{{ "货物体积：" + scope.row.goodsVolume }}</li>
                <li>{{ "货物价值：" + scope.row.goodsValue }}</li>
                <li>{{ "货款：" + scope.row.payment }}</li>
                <li>{{ "里程：" + scope.row.distance }}</li>
              </ul>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
      <el-table-column
        label="是否有易碎品"
        align="center"
        prop="containsFragileGoods"
      >
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.accept_contains_fragile_goods_yes_no"
            :value="scope.row.agencyStatus"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="是否预收货款"
        align="center"
        prop="isNeedCollectPayment"
      >
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.customer_is_need_collect_payment"
            :value="scope.row.isNeedCollectPayment"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="发货时间"
        align="center"
        prop="deliveryTime"
        width="150"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.deliveryTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注信息" align="center" prop="remarks" />
      <el-table-column
        fixed="right"
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button size="mini" type="text">
            <router-link
              :to="{
                path: 'complaint/statics',
                query: { orderNumber: scope.row.orderNumber },
              }"
              class="link-type"
            >
              <span>投诉</span>
            </router-link>
            <!-- <router-link :to="'/complaint/statics' + '?'+scope.row.orderNumber" class="link-type">
            <span>投诉</span>
          </router-link> -->
          </el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleDelete(scope.row)"
            v-hasPermi="['accept:contract:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="发货人姓名" prop="senderName">
          <el-input v-model="form.senderName" placeholder="请输入发货人姓名" />
        </el-form-item>
        <el-form-item label="发货人电话" prop="senderPhone">
          <el-input v-model="form.senderPhone" placeholder="请输入发货人电话" />
        </el-form-item>
        <el-form-item label="发货人地址" prop="senderAddr">
          <el-input v-model="form.senderAddr" placeholder="请输入发货人地址" />
        </el-form-item>
        <el-form-item label="收货人姓名" prop="receiverName">
          <el-input
            v-model="form.receiverName"
            placeholder="请输入收货人姓名"
          />
        </el-form-item>
        <el-form-item label="收货人电话" prop="receiverPhone">
          <el-input
            v-model="form.receiverPhone"
            placeholder="请输入收货人电话"
          />
        </el-form-item>
        <el-form-item label="收货人地址" prop="receiverAddr">
          <el-input
            v-model="form.receiverAddr"
            placeholder="请输入收货人地址"
          />
        </el-form-item>
        <el-form-item label="是否预收货款" prop="isNeedCollectPayment">
          <el-select
            v-model="form.isNeedCollectPayment"
            placeholder="请选择是否预收货款"
          >
            <el-option
              v-for="dict in dict.type.customer_is_need_collect_payment"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <div>
          <el-form-item label="货款" prop="payment">
            <el-select v-model="form.payment" placeholder="请选择" clearable>
              <el-option
                v-for="dict in dict.type.customer_payment"
                :key="dict.value"
                :label="dict.label"
                :value="parseInt(dict.value)"
              />
            </el-select>
          </el-form-item>
        </div>
        <el-form-item label="是否有易碎品" prop="containsFragileGoods">
          <el-select
            v-model="queryParams.containsFragileGoods"
            placeholder="请选择"
            clearable
          >
            <el-option
              v-for="dict in dict.type.accept_contains_fragile_goods_yes_no"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="货物重量" prop="goodsWeight">
          <el-input v-model="form.goodsWeight" placeholder="请输入货物重量" />
        </el-form-item>
        <el-form-item label="货物体积" prop="goodsVolume">
          <el-input v-model="form.goodsVolume" placeholder="请输入货物体积" />
        </el-form-item>
        <el-form-item label="货物价值" prop="goodsValue">
          <el-input v-model="form.goodsValue" placeholder="请输入货物价值" />
        </el-form-item>
        <el-form-item label="地图">
          <div id="allmap" style="width: 500px; height: 300px">
            <baidu-map
              class="map"
              :center="map.center"
              :zoom="map.zoom"
              @ready="handler"
            >
              <!--缩放-->
              <bm-navigation anchor="BMAP_ANCHOR_TOP_LEFT"></bm-navigation>
              <!--定位-->
              <bm-geolocation
                anchor="BMAP_ANCHOR_BOTTOM_RIGHT"
                :showAddressBar="true"
                :autoLocation="true"
              ></bm-geolocation>
              <!--点-->
              <bm-marker
                :position="map.center"
                :dragging="map.dragging"
                animation="BMAP_ANIMATION_DROP"
              >
                <!--提示信息-->
                <bm-info-window :show="map.show">Hello~</bm-info-window>
              </bm-marker>
            </baidu-map>
          </div>
        </el-form-item>
        <el-form-item label="备注信息" prop="remarks">
          <el-input v-model="form.remarks" placeholder="请输入备注信息" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {
  listContract,
  getContract,
  delContract,
  addContract,
  updateContract,
} from "@/api/customer/order";
import BMap from "vue-baidu-map";

export default {
  name: "Contract",
  dicts: [
    "accept_collect_payment_yes_no",
    "accept_contains_fragile_goods_yes_no",
    "agency_fund_status_yes_no",
    "sys_yes_no",
    "customer_payment",
    "accept_waybill_status",
    "customer_is_need_collect_payment",
  ],
  data() {
    return {
      map: {
        center: { lng: 121.447254, lat: 31.32362 },
        zoom: 15,
        show: true,
        dragging: true,
      },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      contractList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        waybillNumber: null,
        senderName: null,
        senderPhone: null,
        senderAddr: null,
        receiverName: null,
        receiverPhone: null,
        receiverAddr: null,
        isNeedCollectPayment: null,
        payment: null,
        deliveryTime: null,
        distance: null,
        goodsWeight: null,
        goodsVolume: null,
        containsFragileGoods: null,
        goodsValue: null,
        agencyStatus: null,
        waybillStatus: null,
        remarks: null,
        deleted: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        senderName: [
          { required: true, message: "发货人姓名不能为空", trigger: "blur" },
        ],
        senderPhone: [
          { required: true, message: "发货人电话不能为空", trigger: "blur" },
        ],
        senderAddr: [
          { required: true, message: "发货人地址不能为空", trigger: "blur" },
        ],
        receiverName: [
          { required: true, message: "收货人姓名不能为空", trigger: "blur" },
        ],
        receiverPhone: [
          { required: true, message: "收货人电话不能为空", trigger: "blur" },
        ],
        receiverAddr: [
          { required: true, message: "收货人地址不能为空", trigger: "blur" },
        ],
        agencyStatus: [
          { required: true, message: "结算状态不能为空", trigger: "change" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    handler({ BMap, map }) {
      let me = this;
      console.log(BMap, map);
      // 鼠标缩放
      map.enableScrollWheelZoom(true);
      // 点击事件获取经纬度
      map.addEventListener("click", function (e) {
        console.log(e.point.lng, e.point.lat);
      });
    },

    /** 查询啊列表 */
    getList() {
      this.loading = true;
      listContract(this.queryParams).then((response) => {
        this.contractList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        orderNumber: null,
        waybillNumber: null,
        senderName: null,
        senderPhone: null,
        senderAddr: null,
        receiverName: null,
        receiverPhone: null,
        receiverAddr: null,
        isNeedCollectPayment: null,
        payment: null,
        deliveryTime: null,
        distance: null,
        goodsWeight: null,
        goodsVolume: null,
        containsFragileGoods: null,
        goodsValue: null,
        agencyStatus: null,
        waybillStatus: null,
        remarks: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null,
        deleted: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.orderNumber);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加订单";
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.orderNumber != null) {
            updateContract(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addContract(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const orderNumbers = row.orderNumber || this.ids;
      this.$modal
        .confirm('是否确认删除啊编号为"' + orderNumbers + '"的数据项？')
        .then(function () {
          return delContract(orderNumbers);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "customer/contract/export",
        {
          ...this.queryParams,
        },
        `contract_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>

<style scoped>
.map {
  width: 360px;
  height: 300px;
}
</style>
